package main

import (
	"fmt"
	"math"
)

type ErrNegativeSqrt float64

func (e ErrNegativeSqrt) Error() string {
	return fmt.Sprintf("cannot Sqrt negative number:%g", float64(e))

}

func Sqrt(x, z float64) (float64, error) {
	if x < 0 {
		return 0.0, ErrNegativeSqrt(x)

	} else {
		y := z
		z = z - (math.Pow(z, 2)-x)/(2*z)
		if math.Abs(y-z) > 0.00001 {
			return Sqrt(x, z)

		}

	}
	return z, nil

}

func main() {
	z := 1.0
	fmt.Println(Sqrt(2, z))
	fmt.Println(Sqrt(-2, z))

}
